const express = require('express');
const line = require('../module/line/line');
const router = express.Router();

// 添加所有路线
router.get('/getLine', async(req, res) => {
    const data = await line.getLine(req.query)
    res.json(data)
})

// 添加路线
router.post('/addLine', async (req, res) => {
    const loginStatus = await global.tokenFun.checkToken(req)
    if (loginStatus.loginStatus) {
        const data = await line.addLine({
            ...loginStatus.loginRows,
            ...req.body
        })
        res.json(data)
    }else {
        res.json(loginStatus.msg)
    }
})

// 获取当前用户的路线
router.get('/getMyLine', async(req, res) => {
    const loginStatus = await global.tokenFun.checkToken(req)
    if (loginStatus.loginStatus) {
        const data = await line.getMyLine({
            ...loginStatus.loginRows,
            ...req.query
        })
        res.json(data)
    }else {
        res.json(loginStatus.msg)
    }
})

// 获取路线详情
router.get('/getLineDetail', async(req, res) => {
    const loginStatus = await global.tokenFun.checkToken(req)
    if (loginStatus.loginStatus) {
        const data = await line.getLineDetail({
            ...loginStatus.loginRows,
            ...req.query
        })
        res.json(data)
    }else {
        res.json(loginStatus.msg)
    }
})

// 编辑路线信息
router.post('/editLine', async (req, res) => {
    const loginStatus = await global.tokenFun.checkToken(req)
    if (loginStatus.loginStatus) {
        const data = {}
        res.json(data)
    }else {
        res.json(loginStatus.msg)
    }
})

router.post('/addStep', async (req, res) => {
    const loginStatus = await global.tokenFun.checkToken(req)
    if (loginStatus.loginStatus) {
        const data = await line.saveMarkerSteps({
            ...loginStatus.loginRows,
            ...req.body
        })
        res.json(data)
    }else {
        res.json(loginStatus.msg)
    }
})



// 删除路线
router.delete('/delLine', async (req, res) => {
    const loginStatus = await global.tokenFun.checkToken(req)
    if (loginStatus.loginStatus) {
        const data = await line.delLine(req.query)
        res.json(data)
    }else {
        res.json(loginStatus.msg)
    }
})

// 获取最近编辑的一条路线
router.get('/getLastEditedRoute', async(req, res) => {
    const loginStatus = await global.tokenFun.checkToken(req)
    if (loginStatus.loginStatus) {
        const data = await line.getLastEditedRoute({
            ...loginStatus.loginRows,
            ...req.query
        })
        res.json(data)
    }else {
        res.json(loginStatus.msg)
    }
})

module.exports = router;
